#include "Texture.h" 
#include "Paras.h"
namespace Euclid
{
	Texture::Texture()
	{
		_texture = 0;
		_device = Paras::getSingletonPtr()->_device;
	}

	Texture::~Texture()
	{

	}

	IDirect3DTexture9* Texture::getTexture()
	{
		return _texture;
	}

	bool Texture::loadFromFile( const std::string& fileName )
	{
		HRESULT hr = D3DXCreateTextureFromFile(_device ,
			fileName.c_str(), &_texture);
		if (FAILED(hr))
		{
			std::string dataFile;
			Buddha::FileSystem::getSingletonPtr()->getDataDirectory(dataFile);
			dataFile += "\\";
			//
			std::string path(fileName);
			path = dataFile + fileName;
			HRESULT hr = D3DXCreateTextureFromFile(_device ,
				path.c_str(), &_texture);
			if (FAILED(hr))
			{
				return false;
			}
		}

		return true;
	}

	bool Texture::loadFromMemory( unsigned char* buffer, unsigned int length )
	{
		HRESULT hr;
		hr = D3DXCreateTextureFromFileInMemory(_device, (LPCVOID)buffer, length, &_texture);
		if (FAILED(hr))
		{
			return false;
		}
		return true;
	}

	bool Texture::loadFromRawData( unsigned char* buffer, unsigned int width, unsigned int height, unsigned int pitch, D3DFORMAT format )
	{
		HRESULT hr;
		//
		hr = D3DXCreateTexture(_device, width, height, 1, 0, format, D3DPOOL_MANAGED, &_texture);
		if (FAILED(hr))
		{
			return false;
		}
		//
		IDirect3DSurface9* sur = 0;
		_texture->GetSurfaceLevel(0, &sur);
		if (sur)
		{
			RECT rc = {0, 0, width, height};
			//
			hr = D3DXLoadSurfaceFromMemory(sur, 0, NULL, (void*)buffer, format, pitch, 0, &rc, D3DX_DEFAULT, 0);
			//
			sur->Release();
			//
			if (buffer)
			{
				delete buffer;
				buffer = 0;
			}
		}
		return true;
	}

	bool Texture::createEmpty( unsigned int width, unsigned int height, D3DFORMAT pf )
	{
		HRESULT hr = D3DXCreateTexture(_device, width, height, 1, 0,
			pf, D3DPOOL_MANAGED, &_texture);

		if(FAILED(hr))
		{
			return false;
		}

		//
		return true;
	}

	bool Texture::setSubData( unsigned int level, unsigned int left, unsigned int top, unsigned int width, unsigned int height, unsigned int pitch, void* pData, D3DFORMAT pf )
	{
		if(NULL == _texture)
		{
			return false;
		}

		//
		IDirect3DSurface9 *pSurface = 0;
		_texture->GetSurfaceLevel(0, &pSurface);
		if (NULL == pSurface)
		{
			return false;
		}

		//
		RECT rc = {0, 0, width, height};
		RECT drc = {left, top, width + left, height + top};
		
		//
		HRESULT hr = D3DXLoadSurfaceFromMemory(pSurface, 0, &drc, pData, pf,
			pitch, 0, &rc, D3DX_DEFAULT, 0);
		if(FAILED(hr))
		{
			return false;
		}
		pSurface->Release();

		//
		return false;
	}
}